import { LatLng } from 'leaflet';

import Layer from './layer';
import Point from '../feature/point';
import Feature from '../feature/feature';
import { FeatureJson, LayerOption } from '../types';
import { generateId, getAnotation } from '../util/util';

class PointsLayer extends Layer {
    draggable: boolean = false;

    constructor(layerOption: LayerOption) {
        super(layerOption, 'point');
    }

    addPoint(id: string, loc: LatLng, props?: any): Feature {
        const point = new Point(id, this.layerOption, loc, {}, props);
        this.add(point);

        return point;
    }

    draw(latLng: LatLng) {
        const id = generateId('point');
        const feature = this.addPoint(id, latLng);
        this.layerOption.changeMode('select', feature);
        this.layerOption.addHistory({ annotation: getAnotation('draw ', feature), features: [feature] });
        this.layerOption.eventBus.emit('drawend', feature.instance());
    }

    render(features: FeatureJson[]): Feature[] {
        const results: Feature[] = [];
        features.forEach((item) => {
            const { id, latLngs, properties } = item;
            const feature = this.addPoint(id, (latLngs as LatLng[])[0], properties);
            results.push(feature);
        });
        return results;
    }
}

export default PointsLayer;
